package com.ruoyi.web.controller.repair;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.repair.domain.TRepairRecord;
import com.ruoyi.repair.service.ITRepairRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 维修记录Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/system/record")
public class TRepairRecordController extends BaseController
{
    @Autowired
    private ITRepairRecordService tRepairRecordService;

    /**
     * 查询维修记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(TRepairRecord tRepairRecord)
    {
        startPage();
        List<TRepairRecord> list = tRepairRecordService.selectTRepairRecordList(tRepairRecord);
        return getDataTable(list);
    }

    /**
     * 导出维修记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "维修记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TRepairRecord tRepairRecord)
    {
        List<TRepairRecord> list = tRepairRecordService.selectTRepairRecordList(tRepairRecord);
        ExcelUtil<TRepairRecord> util = new ExcelUtil<TRepairRecord>(TRepairRecord.class);
        util.exportExcel(response, list, "维修记录数据");
    }

    /**
     * 获取维修记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tRepairRecordService.selectTRepairRecordById(id));
    }

    /**
     * 新增维修记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "维修记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TRepairRecord tRepairRecord)
    {
        return toAjax(tRepairRecordService.insertTRepairRecord(tRepairRecord));
    }

    /**
     * 修改维修记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "维修记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TRepairRecord tRepairRecord)
    {
        return toAjax(tRepairRecordService.updateTRepairRecord(tRepairRecord));
    }

    /**
     * 删除维修记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "维修记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tRepairRecordService.deleteTRepairRecordByIds(ids));
    }

    /**
     * 根据资产编号查询维修履历
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/asset/{assetNo}")
    public TableDataInfo getRepairHistoryByAsset(@PathVariable("assetNo") String assetNo, 
                                                String startDate, String endDate)
    {
        startPage();
        List<TRepairRecord> list = tRepairRecordService.selectRepairHistoryByAsset(assetNo, startDate, endDate);
        return getDataTable(list);
    }

    /**
     * 根据维修单号查询完整维修履历（包含报修信息）
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/work/{workNo}")
    public AjaxResult getCompleteRepairHistoryByWorkNo(@PathVariable("workNo") String workNo)
    {
        List<Object> list = tRepairRecordService.selectCompleteRepairHistoryByWorkNo(workNo);
        return success(list);
    }

    /**
     * 根据资产编号查询完整维修履历（包含报修和维修单信息，即使没有维修记录）
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/asset/complete/{assetNo}")
    public TableDataInfo getCompleteRepairHistoryByAsset(@PathVariable("assetNo") String assetNo, 
                                                        String startDate, String endDate)
    {
        startPage();
        List<Object> list = tRepairRecordService.selectCompleteRepairHistoryByAsset(assetNo, startDate, endDate);
        return getDataTable(list);
    }
}
